<?php
/**
 * Created by jinqiang on 2025/4/8 15:57.
 * Copyright (c) 2025, Your Company. All rights reserved.
 */


namespace App\Http\Controllers\Banshi;


use App\Http\Controllers\Controller;
use App\Http\Services\Banshi\MaterielService;
use Illuminate\Http\Request;

class MaterielController extends Controller
{
    public function list(Request $request, MaterielService $materielService)
    {
        $this->validate($request, [
            'page' => 'required',
            'limit' => 'required'
        ]);
        $param = $request->all();
        if(isset($param['get_style_bom']) && $param['get_style_bom'] != ""){//获取style_bom下的物料清单
            $res = $materielService->searchStyleBom($param);
        }else{
            $res = $materielService->search($param);
        }
        return result($res);
    }

    //获取物料分类级联结构
    public function category(MaterielService $materielService)
    {
        return result($materielService->getMaterielCategory());
    }

    //物料档案删除
    public function del(Request $request, MaterielService $materielService)
    {
        $this->validate($request, [
            'id' => 'required',
        ]);
        $param = $request->all();
        $res = $materielService->del((Int)$param['id']);
        return $res['code'] === 0 ? success("删除成功") : error($res['msg'], $res);
    }

    //物料档案保存
    public function save(Request $request, MaterielService $materielService)
    {
        $this->validate($request, [
            'data' => 'required',
        ]);
        $param = $request->all();
        $res = $materielService->save(json_decode($param['data'], true));
        return $res['code'] === 0 ? success("保存成功") : error($res['msg'], $res);
    }
}
